using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 学生缴费管理控制器
    /// </summary>
    /// <remarks>
    /// 该控制器提供学生缴费信息的增删改查功能
    /// 包括分页查询、详情查询、创建、更新和删除操作
    /// </remarks>
    [Authorize]
    //[RequirePermission("student")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class StudentPaymentController : ApiControllerBase
    {
        private readonly IStudentPaymentService _studentPaymentService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="studentPaymentService">学生缴费服务</param>
        public StudentPaymentController(IStudentPaymentService studentPaymentService)
        {
            _studentPaymentService = studentPaymentService;
        }

        /// <summary>
        /// 获取学生缴费分页列表
        /// </summary>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns>学生缴费分页列表</returns>
        /// <remarks>
        /// 支持按学生ID、教师课表组ID、缴费状态等条件筛选
        /// </remarks>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<StudentPaymentDto>>> GetPagedList([FromBody] StudentPaymentPageRequest pageRequest)
        {
            var result = await _studentPaymentService.GetPagedListAsync(pageRequest);
            return ApiResponse<PagedResult<StudentPaymentDto>>.Ok(result);
        }

        /// <summary>
        /// 获取学生缴费详情
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>学生缴费详情</returns>
        [HttpPost]
        public async Task<ApiResponse<StudentPaymentDto>> GetById([FromBody] IdRequest request)
        {
            var payment = await _studentPaymentService.GetByIdAsync(request.Id);
            return ApiResponse<StudentPaymentDto>.Ok(payment);
        }

        /// <summary>
        /// 创建学生缴费记录
        /// </summary>
        /// <param name="input">创建学生缴费请求参数</param>
        /// <returns>创建后的学生缴费信息</returns>
        [HttpPost]
        [RequirePermission("student")] // 指定所需的权限
        public async Task<ApiResponse<StudentPaymentDto>> Create([FromBody] CreateStudentPaymentDto input)
        {
            var payment = await _studentPaymentService.CreateAsync(input);
            return ApiResponse<StudentPaymentDto>.Ok(payment);
        }

        /// <summary>
        /// 批量创建学生缴费记录
        /// </summary>
        /// <param name="inputs">批量创建学生缴费请求参数</param>
        /// <returns>创建后的学生缴费信息列表</returns>
        [HttpPost]
        [RequirePermission("student")] // 指定所需的权限
        public async Task<ApiResponse<List<StudentPaymentDto>>> CreateRange([FromBody] List<CreateStudentPaymentDto> inputs)
        {
            var payments = await _studentPaymentService.CreateRangeAsync(inputs);
            return ApiResponse<List<StudentPaymentDto>>.Ok(payments);
        }

        /// <summary>
        /// 更新学生缴费记录
        /// </summary>
        /// <param name="input">更新学生缴费请求参数</param>
        /// <returns>更新后的学生缴费信息</returns>
        [HttpPost]
        [RequirePermission("student")] // 指定所需的权限
        public async Task<ApiResponse<StudentPaymentDto>> Update([FromBody] UpdateStudentPaymentDto input)
        {
            var payment = await _studentPaymentService.UpdateAsync(input);
            return ApiResponse<StudentPaymentDto>.Ok(payment);
        }

        /// <summary>
        /// 删除学生缴费记录
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [RequirePermission("student")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _studentPaymentService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 获取指定学生的所有缴费记录
        /// </summary>
        /// <param name="request">ID请求参数</param>
        /// <returns>学生缴费记录列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<StudentPaymentDto>>> GetByStudentId([FromBody] IdRequest request)
        {
            var payments = await _studentPaymentService.GetByStudentIdAsync(request.Id);
            return ApiResponse<List<StudentPaymentDto>>.Ok(payments);
        }
    }
} 